﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DeviceHelloWorld
{
    public partial class CountdownForm : Form
    {
        private bool isCountingDown = false;
        private DateTime timeToEnd;
    
        public CountdownForm()
        {
            InitializeComponent();
            PopulateListBox(listBoxHours, 0, 24);
            PopulateListBox(listBoxMinutes, 0, 59);
            PopulateListBox(listBoxSeconds, 0, 59);

            isCountingDown = false;
            StartStop();
        }

        private void PopulateListBox(ListBox lb, int min, int max)
        {
            lb.BeginUpdate();
            lb.Items.Clear();
            for (int i = min; i <= max; i++)
            {
                lb.Items.Add(i);
            }
            lb.SelectedIndex = 0;
            lb.EndUpdate();
        }

        private void StartStopOld()
        {
            // flip whatever state we were in
            isCountingDown = !isCountingDown;
        }

        private void StartStop()
        {
            //buttonStartStop.Text = (isCountingDown) ? "Stop" : "Start";
            labelCountDown.Text = " --- ";
            if ( isCountingDown ) 
            {
                //MessageBox.Show(listBoxSeconds.SelectedIndex.ToString());
                timeToEnd = DateTime.Now.AddHours(   Convert.ToDouble(listBoxHours.SelectedIndex ) 
                                       ).AddMinutes( Convert.ToDouble(listBoxMinutes.SelectedIndex) 
                                       ).AddSeconds( Convert.ToDouble(listBoxSeconds.SelectedIndex));
            }

            timerCountDown.Enabled = isCountingDown;
            listBoxHours.Visible = !isCountingDown;
            listBoxMinutes.Visible = !isCountingDown;
            listBoxSeconds.Visible = !isCountingDown;
            labelCountDown.Visible = isCountingDown;
            lblHours.Visible =
            lblMinutes.Visible =
            lblSeconds.Visible = !isCountingDown;
            menuItemStart.Enabled = !isCountingDown;
            menuItemStop.Enabled = isCountingDown;
            //timerCountDown_Tick(null, null);
        }

        private void timerCountDown_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = timeToEnd - DateTime.Now;
            int countdownHours = ts.Hours;
            int countdownMinutes = ts.Minutes;
            int countdownSeconds = ts.Seconds;
            if (countdownHours <= 0 && countdownMinutes <= 0 && countdownSeconds <= 0)
            {
                labelCountDown.Text = "Finished!";
                isCountingDown = false;
                StartStop();
                notification.Visible = true;
                // todo: play sound or something.
            }
            else
            {
                updateDisplay(countdownHours, countdownMinutes, countdownSeconds);
            }
        }// timerCountDown_Tick

        private void updateDisplay(int countdownHours, int countdownMinutes, int countdownSeconds )
        {
            labelCountDown.Text = String.Format("{0:00}:{1:00}:{2:00}",
                countdownHours,
                countdownMinutes,
                countdownSeconds);
        }// updateDisplay

        private void menuItemStart_Click(object sender, EventArgs e)
        {
            isCountingDown = true;
            StartStop();
        }// start

        private void menuItemStop_Click(object sender, EventArgs e)
        {
            isCountingDown = false;
            StartStop();
        }


    }
}